import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';

Widget JmPackageImage(String name,
    {double? width, double? height, BoxFit? fit, Color? color}) {
  var filename = name;
  return Image.asset(
    filename,
    height: height,
    width: width,
    fit: fit,
    //colorBlendMode: BlendMode.dstIn,
    color: color,
    package: "jm_flutter",
  );
}

enum MyImageType {
  asset,
  network,
  file,
  memory,
}

class MyImage extends StatelessWidget {
  final String path;
  final MyImageType type;
  final Uint8List? bytes;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final Color? color;
  final bool gaplessPlayback;
  final String? package;

  const MyImage(
    this.path, {
    super.key,
    this.width,
    this.height,
    this.fit,
    this.color,
    this.gaplessPlayback = false,
    this.type = MyImageType.asset,
    this.bytes,
    this.package,
  });

  factory MyImage.asset(String name,
      {double? width,
      double? height,
      BoxFit? fit,
      Color? color,
      bool gaplessPlayback = false,
      String? package}) {
    return MyImage(name,
        width: width,
        height: height,
        fit: fit,
        color: color,
        gaplessPlayback: gaplessPlayback,
        type: MyImageType.asset,
        package: package);
  }

  factory MyImage.network(String url,
      {double? width,
      double? height,
      BoxFit? fit,
      Color? color,
      bool gaplessPlayback = false}) {
    return MyImage(url,
        width: width,
        height: height,
        fit: fit,
        color: color,
        gaplessPlayback: gaplessPlayback,
        type: MyImageType.network);
  }

  factory MyImage.file(String path,
      {double? width,
      double? height,
      BoxFit? fit,
      Color? color,
      bool gaplessPlayback = false}) {
    return MyImage(path,
        width: width,
        height: height,
        fit: fit,
        color: color,
        gaplessPlayback: gaplessPlayback,
        type: MyImageType.file);
  }
  factory MyImage.memory(Uint8List bytes,
      {double? width,
      double? height,
      BoxFit? fit,
      Color? color,
      bool gaplessPlayback = false}) {
    return MyImage("",
        width: width,
        height: height,
        fit: fit,
        color: color,
        gaplessPlayback: gaplessPlayback,
        type: MyImageType.memory,
        bytes: bytes);
  }

  @override
  Widget build(BuildContext context) {
    switch (type) {
      case MyImageType.asset:
        return Image.asset(
          path,
          height: height,
          width: width,
          fit: fit,
          //colorBlendMode: BlendMode.dstIn,
          color: color,
          gaplessPlayback: gaplessPlayback,
          package: package,
        );
      case MyImageType.network:
        return Image.network(
          path,
          height: height,
          width: width,
          fit: fit,
          //colorBlendMode: BlendMode.dstIn,
          color: color,
          gaplessPlayback: gaplessPlayback,
        );
      case MyImageType.file:
        return Image.file(
          File(path),
          height: height,
          width: width,
          fit: fit,
          //colorBlendMode: BlendMode.dstIn,
          color: color,
          gaplessPlayback: gaplessPlayback,
        );
      case MyImageType.memory:
        if (bytes == null) {
          return const SizedBox();
        }
        return Image.memory(
          bytes!,
          height: height,
          width: width,
          fit: fit,
          //colorBlendMode: BlendMode.dstIn,
          color: color,
          gaplessPlayback: gaplessPlayback,
        );
      default:
        return const SizedBox();
    }
  }
}
